import { _decorator, Component, Node, EditBoxComponent, director } from "cc";
import { Tooltip } from "../../prefabs/Tooltip";
import request from "../../utils/request";
const { ccclass, property } = _decorator;

@ccclass("Home/Login")
export class Login extends Component {
    @property(Node)
    private userName: Node = null;

    @property(Node)
    private password: Node = null;

    @property(Node)
    private tooltip: Node = null;

    handleClose() {
        this.node.active = false;
    }

    handleLogin() {
        const userName = this.userName.getComponent(EditBoxComponent).string;
        const password = this.password.getComponent(EditBoxComponent).string;
        console.log(userName, password);
        request.post("/users/login", { userName, password }).then(({ code, message, token }) => {
            if (code !== "0000") {
                this.tooltip.getComponent(Tooltip).show(message);
                return;
            }
            localStorage.setItem("token", token);
            director.loadScene("scenes/server");
        });
    }
}
